package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.constant.ProductConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.feign.GoodsFeign;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.ManageService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 分类与属性控制层
 */

@RestController
@RequestMapping("/admin/product")
public class ManageController {
    @Resource
    private ManageService manageService;

    /**
     * 查询一级分类的值
     * @return
     */
    @GetMapping("/getCategory1")
    public Result getCategory1(){
        return Result.ok(manageService.getBaseCategory1());
    }

    /**
     * 根据一级分类id 获取二级分类
     * @param category1Id
     * @return
     */
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable("category1Id") Long category1Id){
        return Result.ok(manageService.getBaseCategory2(category1Id));
    }

    /**
     * 根据二级分类Id获取三级分类
     * @param category2Id
     * @return
     */
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable("category2Id") Long category2Id){
        return Result.ok(manageService.getBaseCategory3(category2Id));
    }

    /**
     * 获取平台属性列表
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(@PathVariable("category1Id") Long category1Id,
                                  @PathVariable("category2Id") Long category2Id,
                                  @PathVariable("category3Id") Long category3Id){
        return Result.ok(manageService.getAttrInfoList(category3Id));
    }

    /**
     * 添加平台属性
     * @param baseAttrInfo
     * @return
     */
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
       return Result.ok();
    }

    /**
     * 删除平台属性值
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAttrInfo/{id}")
    public Result deleteAttrInfo(@PathVariable Long id){
        manageService.deleteAttrInfo(id);
        return Result.ok();
    }

    /**
     * 获取品牌
     * @return
     */
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        return Result.ok(manageService.getTrademarkList());
    }

    /**
     * 获取商品销售属性名
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        return Result.ok(manageService.getBaseSaleAttrList());
    }

    /**
     * 新增spuInfo
     * @param spuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 分页查询spuInfo
     * @param page
     * @param size
     * @param category3Id
     * @return
     */
    @GetMapping("/{page}/{size}")
    public Result getSpuInfoList(@PathVariable Integer page,
                                 @PathVariable Integer size ,
                                 @RequestParam Long category3Id){
        return Result.ok(manageService.getSpuInfoList(page,size,category3Id));

    }

    /**
     * 分页查询品牌
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/baseTrademark/{page}/{size}")
    public Result getBaseTrademarkList(@PathVariable Integer page,
                                   @PathVariable Integer size){
        return Result.ok(manageService.getBaseTrademarkList(page,size));
    }

    /**
     * 添加品牌
     * @param baseTrademark
     * @return
     */
    @PostMapping("/baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark){
        manageService.saveBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 修改品牌
     * @param baseTrademark
     * @return
     */
    @PutMapping("/baseTrademark/update")
    public Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark){
        manageService.updateBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/baseTrademark/get/{id}")
    public Result getBaseTrademark(@PathVariable Long id){
        return Result.ok(manageService.getBaseTrademark(id));
    }

    /**
     * 删除品牌
     * @param id
     * @return
     */
    @DeleteMapping("/baseTrademark/remove/{id}")
    public Result removeBaseTrademark(@PathVariable Long id){
        manageService.removeBaseTrademark(id);
        return Result.ok();
    }

    /**
     * 分页查询SkuInfo
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list/{page}/{size}")
    public Result getSkuInfoList(@PathVariable Integer page,
                                 @PathVariable Integer size){
        return Result.ok(manageService.getSkuInfoList(page,size));
    }

    /**
     * 根据spuId查询销售属性图片
     * @param spuId
     * @return
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        return Result.ok(manageService.getSpuImageList(spuId));
    }

    /**
     * 根据spuId获取平台属性值
     * @param spuId
     * @return
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable long spuId){
        return Result.ok(manageService.getSpuSaleAttrList(spuId));
    }

    /**
     * 新增/修改skuInfo
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    @Autowired
    private GoodsFeign goodsFeign;
    /**
     * 上架商品
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        manageService.upOrDown(skuId, ProductConst.SKU_ON_SALE);
        return Result.ok();
    }

    /**
     * 下架商品
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        manageService.upOrDown(skuId, ProductConst.SKU_CANCLE_SALE);
        return Result.ok();
    }
}
